$(document).ready(function(){
	
	$("#savePrefBtn").click(function(){
		savePreferences();
	});
	$("#savePrefBtn").button();
	
	getStartOfMonthDate();
	getAutoReconcileData();
	getUserDetails();
	getBudgetItemAccountAssociation();
	getBudgetItemsForUser();
	getAccountsForUser();
	validateInput();
});


function getAccountsForUser(){
	$(".accounts").remove();
	
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "getAccountsForUser";
	parms["username"]= "myself";
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			$(resultData).find("Account").each(function(){
				var accountStr = '<option value="'+ $(this).find("id").text() +'" class="accounts">'+ $(this).find("name").text() +'</option>';
				$("#accountList").append(accountStr);
			});
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function getBudgetItemsForUser(){
	$(".budgetItems").remove();
	
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "getBudgetItemsForUser";
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			$(resultData).find("BudgetItem").each(function(){
				var budgetItemStr = '<option value="'+ $(this).find("id").text() +'" class="budgetItems">'+ $(this).find("description").text() +'</option>';
				$("#budgetItemList").append(budgetItemStr);
			});
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}


function getStartOfMonthDate(){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "getStartOfMonthDate";
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			$("#startOfMonth").val($(resultData).find("firstdayofmonth").text());
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function getAutoReconcileData(){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "getAutoReconcileData";
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			displayAutoReconcileData(resultData);
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function getBudgetItemAccountAssociation(){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "getBudgetItemAccountAssociation";

	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			displayBudgetItemAccountAssociation(resultData);
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function getUserDetails(){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "getUserDetails";
	parms["name"]= $("#name").val();
	parms["surname"]= $("#surname").val();
	parms["username"]= $("#username").val();
	parms["password"]= $("#password").val();
	parms["email"]= $("#emailAdd").val();
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			$("#name").val( $(resultData).find("name").text());
			$("#surname").val( $(resultData).find("surname").text());
			$("#username").val( $(resultData).find("username").text());
			$("#password").val( $(resultData).find("password").text());
			$("#emailAdd").val( $(resultData).find("email").text());
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function deleteAutoReconcile(element){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "deleteAutoReconcile";
	parms["key"]= $(element).parent().parent().find(".key").html();
	parms["value"]= $(element).parent().parent().find(".value").html();
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			location.reload();
			alert("Auto reconcile deleted successfully.");
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function deleteBudgetItemAccountAssociation(element){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "deleteBudgetItemAccountAssociation";
	parms["associationId"]= $(element).parent().parent().attr("id");
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			location.reload();
			alert("Budget item account assoication deleted successfully.");
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function displayBudgetItemAccountAssociation(data){
	$(data).find("KeyValueBean").each(function(){
		var rowStr = '<tr id="'+ $(this).find("id").text() +'"><td class="key">'+ $(this).find("value").text() +'</td><td class="value">'+ $(this).find("key").text() +'</td>'
		+'<td><a href="#" onclick="deleteBudgetItemAccountAssociation(this);"><img src="images/delete.png" title="Delete this row" alt="Delete this row" border="0" width="10"/></a></td></tr>';
		$(rowStr).insertAfter($("#budgetItemAccountHeader"));
	});
}

function displayAutoReconcileData(data){
	$(data).find("AutoReconcile").each(function(){
		var rowStr = '<tr id="'+ $(this).find("bugetitemid").text() +'"><td class="key">'+ $(this).find("key").text() +'</td><td class="value">'+ $(this).find("value").text() +'</td>'
		+'<td><a href="#" onclick="deleteAutoReconcile(this);"><img src="images/delete.png" title="Delete this row" alt="Delete this row" border="0" width="10"/></a></td></tr>';
		$(rowStr).insertAfter($("#autoReconcileHeaderRow"));
	});
}

function savePreferences(){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "savePreferences";
	parms["startOfMonthDate"]= $("#startOfMonth").val();
	parms["name"]= $("#name").val();
	parms["surname"]= $("#surname").val();
	parms["username"]= $("#username").val();
	parms["password"]= $("#password").val();
	parms["email"]= $("#emailAdd").val();
	parms["accountId"]= $("#accountList").val();
	parms["budgetitemId"]= $("#budgetItemList").val();
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			location.reload();
			alert("Preferences saved successfully.");
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}